package com.nashuju.web.controller.article;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.durcframework.core.DefaultGridResult;
import org.durcframework.core.MessageResult;
import org.durcframework.core.controller.CrudController;
import org.durcframework.core.expression.Expression;
import org.durcframework.core.expression.ExpressionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nashuju.web.entity.article.ArticleBase;
import com.nashuju.web.entity.article.ArticleBaseSch;
import com.nashuju.web.entity.article.ArticleBaseSchGtId;
import com.nashuju.web.entity.article.ArticleBaseSchLtId;
import com.nashuju.web.entity.article.ArticleExtend;
import com.nashuju.web.entity.sys.SysUpload;
import com.nashuju.web.service.article.ArticleBaseService;
import com.nashuju.web.service.article.ArticleExtendService;
import com.nashuju.web.service.sys.SysUploadService;

@Controller
public class ArticleBaseController extends CrudController<ArticleBase, ArticleBaseService> {

    @Autowired
    private ArticleExtendService cArticleExtendService;
    @Autowired
    private SysUploadService cSysUploadService;



    @RequestMapping("/addCArticleBase.do")
    public @ResponseBody MessageResult addCArticleBase(ArticleBase entity, HttpServletRequest request) {
        MessageResult ret = this.save(entity);
        // 判断扩展字段，并进行遍历
        String[] extFields = entity.getExtFields().split(",");
        for (int i = 0; i < extFields.length; i++) {
            ArticleExtend cae = new ArticleExtend();
            cae.setAbId(entity.getId());
            cae.setScId(entity.getScId());
            String extFieldName = extFields[i];
            // 如果该扩展字段存在且需要有值，那么则进行入库操作
            if (request.getParameter(extFieldName) != null && request.getParameter(extFieldName).length() > 0) {
                cae.setCellname(extFieldName);
                cae.setCellvalue(request.getParameter(extFieldName));
                cArticleExtendService.save(cae);
            }
        }
        return ret;
    }

//    @RequestMapping("/listCArticleBase.do")
//    public @ResponseBody GridResult listCArticleBase(ArticleBaseSch searchEntity) {
//        return this.query(searchEntity);
//    }

    @RequestMapping("/listCArticleBase.do")
    public @ResponseBody Object listCArticleBase(ArticleBaseSch searchEntity) {
        DefaultGridResult resultGrid = (DefaultGridResult) this.query(searchEntity);
        for (int i = 0; i < resultGrid.getRows().size(); i++) {
            String idList = ((ArticleBase) resultGrid.getRows().get(i)).getPic();
            ArrayList<SysUpload> uploadList = cSysUploadService.findUpload(idList);
            ((ArticleBase) resultGrid.getRows().get(i)).setUploadList(uploadList);
        }
        return resultGrid;
    }

    @RequestMapping("/listCArticleBaseLast.do")
    public @ResponseBody Object listCArticleBaseLast(ArticleBaseSchGtId searchEntity) {
        DefaultGridResult resultGrid = (DefaultGridResult) this.query(searchEntity);
        for (int i = 0; i < resultGrid.getRows().size(); i++) {
            String idList = ((ArticleBase) resultGrid.getRows().get(i)).getPic();
            ArrayList<SysUpload> uploadList = cSysUploadService.findUpload(idList);
            ((ArticleBase) resultGrid.getRows().get(i)).setUploadList(uploadList);
        }
        return resultGrid;
    }

    @RequestMapping("/listCArticleBaseOld.do")
    public @ResponseBody Object listCArticleBaseOld(ArticleBaseSchLtId searchEntity) {
        DefaultGridResult resultGrid = (DefaultGridResult) this.query(searchEntity);
        for (int i = 0; i < resultGrid.getRows().size(); i++) {
            String idList = ((ArticleBase) resultGrid.getRows().get(i)).getPic();
            ArrayList<SysUpload> uploadList = cSysUploadService.findUpload(idList);
            ((ArticleBase) resultGrid.getRows().get(i)).setUploadList(uploadList);
        }
        return resultGrid;
    }

    @RequestMapping("/getCArticleBaseByExpression.do")
    public @ResponseBody Object getCArticleBaseByexpression(ArticleBaseSch searchEntity) {
        DefaultGridResult resultGrid = (DefaultGridResult) this.query(searchEntity);
        @SuppressWarnings("unchecked")
        List<ArticleBaseSch> rows = (List<ArticleBaseSch>) resultGrid.getRows();
        return rows;
    }

    @RequestMapping("/updateCArticleBase.do")
    public @ResponseBody MessageResult updateCArticleBase(ArticleBase entity, HttpServletRequest request) {
        MessageResult ret = this.update(entity);
        // 扩展字段操作，目前的处理方式是，先将扩展字段中该基本信息所属扩展字段的内容都先全部删除，然后将新值进行重新入表的操作方式
        cArticleExtendService.delABExtByabId(entity.getId());
        // 判断扩展字段，并进行遍历
        String[] extFields = entity.getExtFields().split(",");
        for (int i = 0; i < extFields.length; i++) {
            ArticleExtend cae = new ArticleExtend();
            cae.setAbId(entity.getId());
            cae.setScId(entity.getScId());
            String extFieldName = extFields[i];
            // 如果该扩展字段存在且需要有值，那么则进行入库操作
            if (request.getParameter(extFieldName) != null && request.getParameter(extFieldName).length() > 0) {
                cae.setCellname(extFieldName);
                cae.setCellvalue(request.getParameter(extFieldName));
                cArticleExtendService.save(cae);
            }
        }
        return ret;
    }

    @RequestMapping("/delCArticleBase.do")
    public @ResponseBody MessageResult delCArticleBase(ArticleBase entity) {
        return this.delete(entity);
    }

    @RequestMapping("/delCArticleBaseByBatchIds.do")
    public @ResponseBody MessageResult delCArticleBaseByBatchIds(ArticleBase entity) {
        this.getService().delByBatchIds(entity);
        return success();
    }

    @RequestMapping("/updateCArticleBaseByBatchIds.do")
    public @ResponseBody MessageResult updateCArticleBaseByBatchIds(ArticleBase entity) {
        this.getService().updateByBatchIds(entity);
        return success();
    }

    @RequestMapping("/getCArticleBaseById.do")
    public @ResponseBody ArticleBase getCArticleBaseById(ArticleBase entity) {
        return this.getService().get(entity.getId());
    }

    @RequestMapping("/listCArticleBaseAll.do")
    public @ResponseBody Object listCArticleBaseAll(ArticleBase entity) {
        ExpressionQuery query = ExpressionQuery.buildQueryAll();
        List<Expression> expressions = new ArrayList<Expression>();
        query.addAll(expressions);
        // query.addSort("id", "asc");
        DefaultGridResult resultGrid = (DefaultGridResult) this.queryAll(query);
        @SuppressWarnings("unchecked")
        List<ArticleBase> rows = (List<ArticleBase>) resultGrid.getRows();
        return rows;
    }

}
